package com.i2863.receipt.service.impl;

import com.i2863.receipt.dao.ReceiptRepository;
import com.i2863.receipt.entity.Receipt;
import com.i2863.receipt.service.ReceiptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/8.
 */
@Service
public class ReceiptServiceImpl implements ReceiptService{

    @Resource
    private ReceiptRepository receiptRepository;

    @Override
    public Receipt findOne(Integer id) {
        return receiptRepository.findOne(id);
    }

    @Override
    public  List<Integer> findByActivityId(Integer activityId) {
        return receiptRepository.findByActivityId(activityId);
    }

    @Override
    public List<Receipt> findByIdList(final List<Integer> idList) {
        return receiptRepository.findByIdIn(idList);
    }

    @Override
    public void add(Receipt receipt) {
        receiptRepository.save(receipt);
    }

    @Override
    public void del(Integer id) {
        receiptRepository.delete(id);
    }

    @Override
    public void update(Receipt receipt) {
        receiptRepository.saveAndFlush(receipt);
    }

    @Override
    public Receipt findByCode(String code) {
        return receiptRepository.findByCode(code);
    }

    @Override
    public Integer countByActivityId(Integer activityId) {
        return receiptRepository.countByActivityId(activityId);
    }

    @Override
    public List<Receipt> findByActivityId(Integer activityId,Pageable pageable) {
        return receiptRepository.findByActivityIdOrderByIdAsc(activityId,pageable);
    }

    @Override
    public Page<Receipt> queryByPage(final String startTime, final String endTime, final Integer siteId, Integer pageSize, Integer pageNum,
                                     final String queryContent, final Integer activityId) {
        //构建分页工具
        Pageable pageable = new PageRequest(pageNum-1,pageSize, Sort.Direction.DESC,"receiptDate");//jpa从0开始

        Specification<Receipt> specification = new Specification<Receipt>() {
            //断言集合
            List<Predicate> predicates = new ArrayList<Predicate>();
            @Override
            public Predicate toPredicate(Root<Receipt> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                if(StringUtils.isNotBlank(queryContent)){ //添加断言
                    Predicate likeName = cb.like(root.get("code").as(String.class),"%"+queryContent+"%");
                    predicates.add(likeName);
                }
                if(StringUtils.isNotBlank(startTime)){
                    Predicate start = cb.greaterThanOrEqualTo(root.get("receiptDate").as(String.class),startTime);
                    predicates.add(start);
                }
                if(StringUtils.isNotBlank(endTime)){
                    Predicate end = cb.lessThanOrEqualTo(root.get("receiptDate").as(String.class),endTime);
                    predicates.add(end);
                }
                    Predicate activity = cb.equal(root.get("activity").get("id").as(Integer.class),activityId);
                    predicates.add(activity);
                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return receiptRepository.findAll(specification,pageable);
    }


    @Override
    public void del(List<Receipt> list) {
        receiptRepository.delete(list);
    }
}

